package com.muzi.weshop.view.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.muzi.weshop.R;
import com.muzi.weshop.common.base.BaseActivity;
import com.muzi.weshop.common.contants.RequestCodeContants;
import com.muzi.weshop.model.OrderListModel;
import com.muzi.weshop.model.OrderMsgDtoListBean;
import com.muzi.weshop.view.adapter.OrderListAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.rubintry.dialog.ios.IOSMessageDialog;
import cn.rubintry.speak.OkSpeaker;

/**
 * @author logcat
 */
public class OrderListActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {


    @BindView(R.id.rvOrderList)
    RecyclerView rvOrderList;
    @BindView(R.id.refreshOrderList)
    SmartRefreshLayout refreshOrderList;
    private OrderListAdapter orderListAdapter;
    private List<OrderMsgDtoListBean> orderList;

    private int page;

    @Override
    protected boolean hideStatusBar() {
        return true;
    }

    @Override
    protected int attachedLayout() {
        return R.layout.activity_order_list;
    }

    @Override
    protected void initViews() {
        page = 1;
        rvOrderList.setLayoutManager(new LinearLayoutManager(this));
        rvOrderList.setNestedScrollingEnabled(false);
        orderList = new ArrayList<>();
        orderListAdapter = new OrderListAdapter(R.layout.item_my_order_list , orderList , this);
        rvOrderList.setAdapter(orderListAdapter);
        orderListAdapter.setOnItemClickListener(new OrderListAdapter.OnItemClickListener() {
            @Override
            public void onAddressClick(OrderMsgDtoListBean info) {
                new IOSMessageDialog.Builder(OrderListActivity.this)
                        .setTitle("信息")
                        .setCancelable(true)//点击外部取消
                        .setMessage("地址：" + info.getAdress() + "\n" + "电话号码：" + info.getAccount())
                        .setOnButtonClickListener(new IOSMessageDialog.OnButtonClickListener() {
                            @Override
                            public void onConfirm() {
                                //点击确定
                            }

                            @Override
                            public void onCancel() {
                                //点击取消
                            }
                        }).create().show();
            }

            @Override
            public void onCancel(int id) {
                new IOSMessageDialog.Builder(OrderListActivity.this)
                        .setMessage("确定要取消订单吗？")
                        .setOnButtonClickListener(new IOSMessageDialog.OnButtonClickListener() {
                            @Override
                            public void onConfirm() {
                                //用户取消订单
                                apiPresenter.changeOrderStatus(id , "3" , RequestCodeContants.CANCEL_ORDER);
                            }

                            @Override
                            public void onCancel() {

                            }
                        }).create().show();
            }

            @Override
            public void onReceiveClick(int id) {
                apiPresenter.changeOrderStatus(id , "1" , RequestCodeContants.RECEIVE_ORDER);
            }
        });

        refreshOrderList.setOnRefreshListener(this);
        refreshOrderList.setOnLoadMoreListener(this);
    }

    @Override
    protected void requestData() {
        apiPresenter.getOrderList(page , 1000 , RequestCodeContants.GET_ORDER_LIST);
    }


    @OnClick({R.id.imgBtnBack})
    void onClick(View view){
        switch (view.getId()){
            case R.id.imgBtnBack:
                finish();
                break;
                default:
                    break;
        }
    }

    @Override
    public void onNext(Object o, int requestCode) {
        super.onNext(o, requestCode);
        refreshOrderList.finishRefresh();
        refreshOrderList.finishLoadMore();
        switch (requestCode){
            case RequestCodeContants.GET_ORDER_LIST:
                OrderListModel orderListModel = (OrderListModel) o;
                if(orderListModel == null){
                    return;
                }

                if(orderListModel.getOrderMsgDtoList() == null){
                    return;
                }
                orderList.clear();
                orderList.addAll(orderListModel.getOrderMsgDtoList());
                Collections.sort(orderList, new Comparator<OrderMsgDtoListBean>() {
                    @Override
                    public int compare(OrderMsgDtoListBean o1, OrderMsgDtoListBean o2) {
                        return o1.getStep().compareTo(o2.getStep());
                    }
                });
                orderListAdapter.setNewData(orderList);
                break;
            case RequestCodeContants.GET_ORDER_LIST_LOAD_MORE:
                OrderListModel orderListModelLoadMore = (OrderListModel) o;
                if(orderListModelLoadMore.getOrderMsgDtoList() != null){
                    orderListAdapter.addData(orderListModelLoadMore.getOrderMsgDtoList());  //上拉加载的新数据
                }
                break;
            case RequestCodeContants.CANCEL_ORDER:
                ToastUtils.showShort("取消成功");
                requestData();
                break;
            case RequestCodeContants.RECEIVE_ORDER:
                ToastUtils.showShort("接单成功");
                OkSpeaker.get().speak("接单成功");
                requestData();
                break;
                default:
                    break;
        }
    }

    @Override
    public void onError(Throwable e, int request) {
        super.onError(e, request);
        refreshOrderList.finishRefresh();
        refreshOrderList.finishLoadMore();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        apiPresenter.getOrderList(page , 1000 , RequestCodeContants.GET_ORDER_LIST);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page ++;
        apiPresenter.getOrderList(page , 10 , RequestCodeContants.GET_ORDER_LIST_LOAD_MORE);
    }
}
